package com.canbot.u05.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.canbot.u05.R;

/**
 * 倒计时自动取消对话框
 */
public class TimeCenterDialog extends Dialog {

        private Context mContext;

        private String mTitleText;

        private String mLeftBtnText;

        private String mRightBtnText;

        private TextView mTVMsg;

        private String mString;

        private IButtonClickListerner mClickListenerInterface;

        private int mBkBtnLeft = 0, mBkBtnRight = 0;

        private final String TAG = "CenterDialog";

        private TimeCount time;

        Button mBtnLeft;

        Button mBtnRight;

        private OnFinish mListener;

        /**
         * @param context
         * @param style
         * @param titleText          提示音的文本
         * @param leftBtnText        左边btn的文本
         * @param rightBtnText       右边btn的文本
         * @param leftBtnDrawableID  左边btn的背景
         * @param rightBtnDrawableID 右边btn的背景
         * @param IClickListener     点击事件的监听器
         */
        public TimeCenterDialog(Context context, int style, String titleText,
                                String leftBtnText, String rightBtnText, OnFinish listener,
                                IButtonClickListerner IClickListener) {
                super(context, style);
                this.mContext = context;
                this.mTitleText = titleText;
                this.mLeftBtnText = leftBtnText;
                this.mRightBtnText = rightBtnText;
                this.mClickListenerInterface = IClickListener;
                this.mListener = listener;
                time = new TimeCount(15 * 1000, 1000);//构造CountDownTimer对象
        }

        /**
         * btn采用默认的构造采用 *
         *
         * @param context
         * @param style
         * @param titleText
         * @param leftBtnText
         * @param rightBtnText
         * @param IClickListener
         */
        public TimeCenterDialog(Context context, int style, String titleText,
                                IButtonClickListerner IClickListener) {
                super(context, style);
                this.mContext = context;
                this.mTitleText = titleText;
                this.mClickListenerInterface = IClickListener;
                time = new TimeCount(15 * 1000, 1000);//构造CountDownTimer对象
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                initView();
        }

        private void initView() {
                LayoutInflater layoutInflater = LayoutInflater.from(mContext);
                View viewDialog = layoutInflater.inflate(R.layout.dialog_time_center, null);
                setContentView(viewDialog);

                setCanceledOnTouchOutside(false);
                TextView mTextView = (TextView) viewDialog
                        .findViewById(R.id.tv_dialogtitle);
                mBtnLeft = (Button) viewDialog.findViewById(R.id.btn_dialogleft);
                mBtnRight = (Button) viewDialog
                        .findViewById(R.id.btn_dialogright);
                mTextView.setText(mTitleText);
                mBtnLeft.setText(mLeftBtnText);
                mBtnRight.setText(mRightBtnText);
//		if (mBkBtnLeft != 0) {
//			mBtnLeft.setBackgroundResource(mBkBtnLeft);
//		}
//		if (mBkBtnRight != 0) {
//			mBtnRight.setBackgroundResource(mBkBtnRight);
//		}
                mBtnLeft.setOnClickListener(new ClickListener());
                mBtnRight.setOnClickListener(new ClickListener());
        }


        @Override
        public void show() {
                time.start();//开始计时
                super.show();
        }

        @Override
        public void cancel() {
                super.cancel();
        }

        private class ClickListener implements android.view.View.OnClickListener {

                @Override
                public void onClick(View v) {
                        cancel();
                        int viewID = v.getId();
                        switch (viewID) {
                                case R.id.btn_dialogright:
                                        if (mClickListenerInterface != null) {
                                                mClickListenerInterface.doBtnRight();
                                        }
                                        break;
                                case R.id.btn_dialogleft:
                                        if (mClickListenerInterface != null) {
                                                mClickListenerInterface.doBtnLeft();
                                        }
                                        break;
                                default:
                                        break;
                        }
                }

        }

        class TimeCount extends CountDownTimer {

                public TimeCount(long millisInFuture, long countDownInterval) {
                        super(millisInFuture, countDownInterval);//参数依次为总时长,和计时的时间间隔
                }

                @Override
                public void onFinish() {//计时完毕时触发
                        mListener.onFinish();
//		       dismiss();; //取消对话框
                }

                @Override
                public void onTick(long millisUntilFinished) {//计时过程显示
                        mBtnLeft.setClickable(true);
                        mBtnLeft.setText(mLeftBtnText + "(" + millisUntilFinished / 1000 + "s)");
                }
        }

        public interface OnFinish {

                /**
                 * 计时完毕
                 */
                void onFinish();
        }
}
